package com.gxa.community.admin.config;

import com.gxa.community.admin.realm.UserRealm;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class ShiroConfig {
    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(getRealm());
        return defaultWebSecurityManager;
    }

    @Bean("shiroFilterFactoryBean")
    public  ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManager){
        //设置安全管理器
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);

        //配置拦截地址
        HashMap<String, String> filters = new HashMap<>();
        filters.put("/**","anon");

        //设置用拦截地址
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filters);

        return shiroFilterFactoryBean;
    }

    @Bean
    public Realm getRealm(){
        return new UserRealm();
    }
}
